package com.audi.view;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

import com.audi.R;

public class EditTimeHourDialog {
	private String[] mListMinutes = { "00", "15", "30", "45" };
	private String[] mListHours = { "01", "02", "03", "04", "05", "06", "07",
			"08", "09", "10", "11", "12" };
	private String[] mListAmOrPm = { "AM", "PM" };
	private ArrayAdapter<String> mAdapterSpinnerMinutes, mAdapterSpinnerHours,
			mAdapterAmOrPm;
	private AudiDialog dialogTimeHour;
	private Button mBtnEditTimeHourCancel, mBtnEditTimeHourOk;
	private Spinner mSpnEditHour, mSpnEditMinutes, mSpnAmOrPm;
	private ImageButton mIbtnHours, mIbtnMinutes;
	private Context context;
	private String strHours, strMinutes, strAmOrPm;

	public EditTimeHourDialog(Context activity) {
		this.context = activity;
	}

	public void dialogEditTimeHour() {
		dialogTimeHour = new AudiDialog(context);
		dialogTimeHour.setLayout(R.layout.dialog_edit_time_hour);
		dialogTimeHour.show();
		getWidget();
		addEventForWidget();

	}

	private void getWidget() {
		mBtnEditTimeHourCancel = (Button) dialogTimeHour
				.findViewById(R.id.btn_edit_hour_cancel);
		mBtnEditTimeHourOk = (Button) dialogTimeHour
				.findViewById(R.id.btn_edit_hour_ok);
		mSpnEditHour = (Spinner) dialogTimeHour
				.findViewById(R.id.spn_hour_edit_hour);
		mSpnEditMinutes = (Spinner) dialogTimeHour
				.findViewById(R.id.spn_minutes_edit_hour);
		mSpnAmOrPm = (Spinner) dialogTimeHour
				.findViewById(R.id.spn_ap_edit_hour);
		mIbtnHours = (ImageButton) dialogTimeHour
				.findViewById(R.id.img_hour_edit_hour);
		mIbtnMinutes = (ImageButton) dialogTimeHour
				.findViewById(R.id.img_minutes_edit_hour);

		mAdapterSpinnerMinutes = new ArrayAdapter<String>(context,
				android.R.layout.simple_spinner_item, mListMinutes);
		mAdapterSpinnerMinutes
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSpnEditMinutes.setAdapter(mAdapterSpinnerMinutes);

		mAdapterAmOrPm = new ArrayAdapter<String>(context,
				android.R.layout.simple_spinner_item, mListAmOrPm);
		mAdapterAmOrPm
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSpnAmOrPm.setAdapter(mAdapterAmOrPm);

		mAdapterSpinnerHours = new ArrayAdapter<String>(context,
				android.R.layout.simple_spinner_item, mListHours);
		mAdapterSpinnerHours
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSpnEditHour.setAdapter(mAdapterSpinnerHours);
	}

	private void addEventForWidget() {
		mIbtnMinutes.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				mSpnEditMinutes.performClick();
				mSpnEditMinutes
						.setOnItemSelectedListener(new OnItemSelectedListener() {

							@Override
							public void onItemSelected(AdapterView<?> arg0,
									View arg1, int arg2, long arg3) {
								strMinutes = mListMinutes[arg2];
							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0) {
								strMinutes = "";
							}
						});
			}
		});
		mIbtnHours.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				mSpnEditHour.performClick();
				mSpnEditHour
						.setOnItemSelectedListener(new OnItemSelectedListener() {

							@Override
							public void onItemSelected(AdapterView<?> arg0,
									View arg1, int arg2, long arg3) {
								strHours = mListHours[arg2];
							}

							@Override
							public void onNothingSelected(AdapterView<?> arg0) {
								strHours = "";
							}
						});
			}
		});

		mSpnAmOrPm.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				strAmOrPm = mListAmOrPm[arg2];
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				strAmOrPm = "";
			}
		});
		mBtnEditTimeHourCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Toast.makeText(context, "Back about 5-3-3-1",
						Toast.LENGTH_SHORT).show();
				dialogTimeHour.dismiss();
			}
		});

		mBtnEditTimeHourOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(
						context,
						"Continue 5-3-2-5: " + "Hours - " + strHours
								+ strAmOrPm + ", Minutes - " + strMinutes,
						Toast.LENGTH_LONG).show();
				dialogTimeHour.dismiss();
			}
		});
	}
}
